package utils;

import java.util.ArrayList;
import java.util.List;
import myinterface.INoeud;

public class Segment implements INoeud{
	private int id;
	private Point debut;
	private Point fin ;
	
	private List<Integer> croisements;
	private List<Integer> polygones;
	
	public Segment(){
		this.croisements = new ArrayList<Integer>();
		this.polygones = new ArrayList<Integer>();
	}
	public Segment(int id,Point deb, Point fin,List<Integer> segments,
			List<Integer> polygones){
		this.id = id;
		this.debut = deb;
		this.fin = fin;
		this.croisements = segments;
		this.polygones = polygones;
	}

	public Segment(int id,Point deb, Point fin){
		this.id = id;
		this.debut = deb;
		this.fin = fin;
		this.croisements = new ArrayList<Integer>();
		this.polygones =  new ArrayList<Integer>();
	}
	
	
	public void addPoint(Point p){
		if(this.debut == null)
			this.debut = p;
		else if(this.fin == null)
			this.fin = p;
	}
	
	public Point getDebut() {
		return debut;
	}

	public void addCroisement(Segment s){
	
		this.croisements.add(s.getId());
	}
	
	public void addPoly(Polygon p){
		this.polygones.add(p.getId());
	}
	
	public void addAllCroisement(List<Segment> ss){
		for(Segment s : ss)
			addCroisement(s);
	}
	
	public void addAllPoly(List<Polygon> ps){
		for(Polygon p : ps)
			addPoly(p);
	}
	
	public void setDebut(Point debut) {
		this.debut = debut;
	}

	public static List<Integer> segmentToId(List<Segment> segments){
		List<Integer> list = new ArrayList<Integer>();
		
		for(Segment s : segments)
			list.add(s.getId());
		
		return list;
	}
	
	public Point getFin() {
		return fin;
	}

	public void setFin(Point fin) {
		this.fin = fin;
	}
	
	public String toString(){
		StringBuffer st = new StringBuffer("\t<chemin id=\""+this.id+"\">\n\n");
	
		// On affiche les points du segments
		st.append("\t\t"+this.debut.toString()+"\n");
		st.append("\t\t"+this.fin.toString()+"\n");
		
		// On affiche les chemins qui parte du segments
		for(Integer s : croisements)
			st.append("\n\t\t<chemin id=\""+ s +"\"/>");

		st.append("\n");
		// On affiche les batiments
		for(Integer b : polygones)
			st.append("\n\t\t<polygone id=\""+ b +"\"/>");
		
		st.append("\n");

		st.append("\n\t</chemin>");
		return st.toString();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Integer> getCroisements() {
		return croisements;
	}

	public void setCroisements(List<Integer> croisements) {
		this.croisements = croisements;
	}

	public List<Integer> getPolygones() {
		return polygones;
	}

	public void setPolygones(List<Integer> polygones) {
		this.polygones = polygones;
	}

	@Override
	public List<Integer> getChemin() {
		return croisements;
	}
	public utils.Point intersection(utils.Segment segSuivant) {
		// TODO Auto-generated method stub
		return null;
	}

}
